#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys
import cPickle as pickle
dataList = []
flag = 0

class CheckLogs:
    def save_logsize(self,LogFile,logsize):
              if os.path.isfile('logsize.db'):
                     read_f=file('logsize.db','rb')
                     table_logsize=pickle.load(read_f)
                     read_f.close()
                     write_f=file('logsize.db','wb')
                     table_logsize[LogFile]=logsize
                     pickle.dump(table_logsize,write_f, True)
                     write_f.close()
              else:  
                   f=file('logsize.db','wb')
                   table_logsize={LogFile:logsize}
                   pickle.dump(table_logsize,f,True)
                   f.close()
    def get_logsize(self,LogFile):
        if os.path.isfile('logsize.db'):
                     f=file('logsize.db','rb')
                     table_logsize=pickle.load(f)
                     if LogFile in table_logsize:
                        return table_logsize[LogFile]
        return 1
    def CheckLog(self,LogFile,Times,Threshold,Operator):
        logsize=self.get_logsize(LogFile)
        if logsize == 1:
             print "111"
             logsize = self.GetLogSize(LogFile)
        print "logsize:%s"%logsize
        diff = self.GetDiffSize(logsize,Times,Threshold)
        print diff
        ret = self.Compare(diff,Operator)
        return ret

    def GetDiffSize(self,logsize,Times,Threshold):
        index = int(Times) - 1
        ret = 0
        global dataList
        global flag
        if int(Times) == len(dataList):
            avgSize = sum(dataList)/int(Times)
            diff = Logsize - avgSize
            ret = diff - Threshold
            dataList[flag] = Logsize
            if 0 <= flag < index:
                flag+1
            elif flag == index:
                flag = 0
        else:
            dataList.append(logsize)
            self.save_logsize(LogFile,logsize)
        print "dataList:"
        print dataList
        return ret

    def Compare(self,Ret,Operator=1):
        equal = 0
        less = 0
        greater = 0
        if Operator == 1:
            greater = 1
        elif Operator == 2:
            equal = 1
        elif Operator == 3:
            less = 1
        if Ret ==0:
            return equal
        elif Ret <0:
            return less
        else:
            return greater

    def GetLogSize(self,LogFile):
        if os.path.isfile(LogFile):
            return os.path.getsize(LogFile)
        else:
            return "False"
if __name__ == "__main__":
    if len(sys.argv) < 3:
        print "$0 LogFile Times Threshold (Operator=1)"
        exit()
    elif '2' == sys.argv[4] or '3' == sys.argv[4]:
        Operator = sys.argv[4]
    else:
        Operator = 1
    LogFile = sys.argv[1]
    Times = sys.argv[2]
    Threshold = sys.argv[3]
    ck = CheckLogs()
    ret = ck.CheckLog(LogFile,Times,Threshold,Operator)
    print ret